- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSolution.py
28 lines (20 loc) · 640 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
classCodec:
short_long= {}
defencode(self, longUrl: str) ->str:
"""Encodes a URL to a shortened URL.
"""
x=hash(longUrl)
shortUrl="p"ifx>0else"n"
x=abs(x)
whilex:
shortUrl+=chr(x%95+32)
x//=95
self.short_long[shortUrl] =longUrl
returnshortUrl
defdecode(self, shortUrl: str) ->str:
"""Decodes a shortened URL to its original URL.
"""
returnself.short_long[shortUrl]
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))